<!--结算订单列表-->
<template>
  <div class="app-container">
    <div class="radio-box">
      <el-radio-group
        v-model="queryParams.state"
        @change="changeStatus"
        size="small"
      >
        <el-radio-button :label="'0'">
          <div>待审核</div>
        </el-radio-button>
        <el-radio-button :label="'2'">
          <div>提现成功</div>
        </el-radio-button>
        <el-radio-button :label="'1'">
          <div>提现驳回</div>
        </el-radio-button>
      </el-radio-group>
    </div>
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="提现申请编号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="提现申请编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading" border
      :data="typeList"
      @selection-change="handleSelectionChange"
      ref="multipleTable"
      size="small"
      height="250px"
    >
      <el-table-column
        label="提现申请编号"
        align="center"
        prop="orderCode"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="操作时间"
        align="center"
        :show-overflow-tooltip="true"
        prop="createTime"
      >
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark">
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公告对话框 -->
    <el-dialog
      title="收入明细"
      :visible.sync="openTwo"
      width="70%"
      append-to-body
    >
      <DetailTable :mxtableData="mxtableData" />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="openTwo = false">提交请求</el-button>
        <el-button @click="openTwo = false">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="收入明细" :visible.sync="open" width="70%" append-to-body>
      <el-row>
        <el-col :span="8">
          <div class="detial-info">
            <span>申请提现订单数 :</span>
            <span>{{ withdraw.totalOrder }}</span>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="detial-info">
            <span>申请提现总额(元):</span>
            <span>{{ withdraw.total }}</span>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="detial-info">
            <span>平台手续费（￥) :</span>
            <span>{{ withdraw.grossMarginMoney }}</span>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <div class="detial-info">
            <span>预计到账金额（￥） :</span>
            <span>{{ withdraw.realityMoney }}</span>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="detial-info">
            <span>提现到银行卡 :</span>
            <span>没找到</span>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="detial-info">
            <span>申请提现父订单数 :</span>
            <span>{{ withdraw.totalFatherOrder }}</span>
          </div>
        </el-col>
      </el-row>
      <!-- <el-row>
        <el-col :span="8">
          <div class="detial-info">
            <span>申请提现子订单数 :</span>
            <span></span>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="detial-info">
            <span>申请提现收入总额(元) :</span>
            <span></span>
          </div>
        </el-col> 
      </el-row>-->
      <DetailTable :mxChildtableData="mxChildtableData" />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">提交请求</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { withdrawal, applyWithdrawal } from "@/api/withdrawal";
import {
  listType,
  getType,
  delType,
  addType,
  updateType,
  refreshCache,
} from "@/api/system/dict/type";
import Editor from "@/components/Editor";
import DetailTable from "@/views/finance/components/detailTable";

export default {
  name: "commodity",
  components: {
    Editor,
    DetailTable,
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      openTwo: false,
      open: false,
      // 类型数据字典
      statusOptions: [],
      // 状态数据字典
      typeOptions: [],
      // 查询参数
      //type申请类型：1.货款提现申请，2.业务部提现申请，3.垫资方货款提现申请，4.店长提现申请
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        state: "0",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "商品名称不能为空", trigger: "blur" },
        ],
        noticeType: [
          { required: true, message: "公告类型不能为空", trigger: "change" },
        ],
      },
      type: "0",
      commodityBrandoptions: [],
      selectionarr: [],
      //详情父亲数据
      mxtableData: [],
      mxChildtableData: [],
      detail: {},
      //订单数据
      withdraw: [],
    };
  },
  mounted() {
    //渠道结算状态 subjoinSettlement -- 渠道
    //货款结算状态 cargoSettlement -- 供应商
    //团长结算状态 terminalSettlement -- 店长
    //订单结算状态 orderSettlement --

    let params = this.$route.params;
    if (params.qd) {
      this.holdParams = "subjoinSettlement";
      this.$set(this.queryParams, "type", 2);
    } else if (params.gys) {
      this.holdParams = "cargoSettlement";
      this.$set(this.queryParams, "type", 3);
    } else if (params.dz) {
      this.holdParams = "terminalSettlement";
      this.$set(this.queryParams, "type", 4);
    }
    this.getList();
  },
  created() {
    this.getDicts("sys_notice_status").then((response) => {
      this.statusOptions = response.data;
    });
    this.getDicts("sys_notice_type").then((response) => {
      this.typeOptions = response.data;
    });
    this.getDicts("pp").then((response) => {
      this.commodityBrandoptions = response.data;
    });
  },
  methods: {
    getstate(row) {
      let str = "";
      if (row.state == "-1") {
        str = "草稿箱";
      } else if (row.state == "0") {
        str = "申请中";
      } else if (row.state == "1") {
        str = "申请成功";
      } else if (row.state == "2") {
        str = "已完成";
      }
      return str;
    },
    handleWithdraw() {
      //分页选中数据
      let selectionarr = this.selectionarr;
      let selectArr = [];
      selectionarr.map((item) => {
        if (item) {
          item.map((child) => {
            selectArr.push(child);
          });
        }
      });
      //父数据
      this.mxtableData = selectArr;
      //规格详情
      let mxChildtableData = [];
      //提现单详情
      let withdraw = {
        total: 0,
        realityMoney: 0,
      };
      //申请提现订单数
      withdraw.totalOrder = selectArr.length;
      //申请提现父订单数
      withdraw.totalFatherOrder = selectArr.length;
      selectArr.map((item) => {
        if (item.detailList) {
          item.detailList.map((child) => {
            //父订单
            child.fatherOrder = item.serialCode;
            //预计收入

            mxChildtableData.push(child);
          });
        }
        //申请提现总额(元)
        withdraw.total += item.totalMoney || 0;
        //平台手续费（￥)
        withdraw.grossMarginMoney = item.withdraw || 0;
        //预计到账金额（￥）
        withdraw.realityMoney += item.realityMoney || 0;
      });
      this.withdraw = withdraw;
      this.mxChildtableData = mxChildtableData;

      if (selectArr.length == 0) {
        this.$message.error("请选择一条提现数据");
        return;
      }
      this.open = true;
    },
    handleAdd() {},
    //   去提现
    getWithdraw() {
      this.open = true;
    },
    // 提现记录
    withdrawalRecord() {
      this.reset();
      this.$router.push({
        path: "/commodity/edit",
        query: {
          type: "reject",
          title: "驳回海关商品备案",
          data: row,
        },
      });
    },
    /** 查询公告列表 */
    handelcopy() {
      copycommodity().then((res) => {
        this.msgSuccess("复制成功");
      });
    },
    /** 查询公告列表 */
    changeStatus(value) {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    getcheck() {
      this.$confirm;
    },
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      var obj = {
        ...this.queryParams,
      };
      withdrawal(obj).then((response) => {
        this.typeList = response.data;
        this.changecurrent();
        this.total = response.total;
        this.loading = false;
      });
    },
    // 公告状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 公告状态字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.noticeType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        noticeTitle: undefined,
        noticeType: undefined,
        noticeContent: undefined,
        status: "0",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
      selection.map((item, index) => {
        item.page = this.queryParams.pageNum;
      });
      this.selectionarr[this.queryParams.pageNum] = selection;
    },
    removalselect(selectionarr, current, selection, index) {
      let selectionarrid = [];
      selectionarr.map((item) => {
        selectionarrid.push(item.dictId);
      });
      if (selectionarr.length == 0) {
        if (selectionarrid.indexOf(current.dictId) == -1) {
          selectionarr[index] = current;
        }
      } else {
      }
    },
    changecurrent() {
      let rows = [];
      let selectionarr = this.selectionarr;
      selectionarr.map((item) => {
        rows.push(...item);
      });
      this.$nextTick(() => {
        this.$refs.multipleTable.clearSelection();
        this.typeList.forEach((outerItem) => {
          rows.forEach((item) => {
            if (outerItem.dictId == item.dictId) {
              this.$refs.multipleTable.toggleRowSelection(outerItem, true);
            }
          });
        });
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.$router.push({
        path: "/commodity/edit",
        query: {
          type: "add",
          title: "新增海关商品备案",
        },
      });
    },
    /** 重新提交 */
    reSubmit(row) {},
    /** 查看打款明细 */
    lookdetial(row) {},
    /** 撤回 */
    recall(row) {},
    /** 审核按钮操作 */
    handleApproval(row) {
      this.openTwo = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.$router.push({
        path: "/commodity/edit",
        query: {
          type: "edit",
          title: "修改海关商品备案",
          data: row,
        },
      });
    },
    handleUpdatereject(row) {
      this.reset();
      this.$router.push({
        path: "/commodity/edit",
        query: {
          type: "reject",
          title: "驳回海关商品备案",
          data: row,
        },
      });
    },
    handlemore(row) {
      this.reset();
      this.$router.push({
        path: "/commodity/edit",
        query: {
          type: "detail",
          title: "海关商品备案详情",
          data: row,
        },
      });
    },
    handlecopy(row) {
      this.$confirm("是否确认复制该商品?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return approvalcommodity(obj);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("复制成功");
        })
        .catch(() => {});
    },
    /** 提交按钮 */
    submitForm: function () {
      let obj = {
        type: Number(4),
        detailList: [],
      };
      let mxtableData = this.mxtableData;
      mxtableData.map((item) => {
        let detailobj = {
          id: item.id,
        };
        obj.detailList.push(detailobj);
      });
      applyWithdrawal(obj).then((res) => {
        if (res.code == 200) {
          this.$message.success("提现成功");
          this.queryParams.pageNum = 1;
          this.getList();
        } else {
          this.$message.success("提现失败");
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除该商品?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delcommodity(ids);
        })
        .then(() => {
          this.queryParams.pageNum = 1;
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    getbrand(row) {
      let commodityBrandoptions = this.commodityBrandoptions;
      let Brand = "";
      commodityBrandoptions.map((item) => {
        if (row.commodityBrand == item.dictValue) {
          Brand = item.dictLabel;
        }
      });
      return Brand;
    },
    getorderState(val) {
      let str = "";
      if (val == "1") {
        str = "待付款";
      } else if (val == "2") {
        str = "代发货";
      } else if (val == "3") {
        str = "待收货";
      } else if (val == "4") {
        str = "退换货";
      } else if (val == "5") {
        str = "已完成";
      } else if (val == "6") {
        str = "退换成功";
      } else if (val == "7") {
        str = "已关闭";
      }
      return str;
    },
  },
};
</script>
<style scoped lang="scss">
.radio-box {
  margin-bottom: 10px;
}
.stock {
  text-decoration: underline;
}
.copy {
  color: #999;
  cursor: pointer;
}
.box {
  display: flex;
  div {
    margin-right: 20px;
  }
}
.dialog-footer {
  display: flex;
  align-items: center;
  justify-content: center;
}
.detial-info {
  padding: 0 10px 10px 10px;
}
.detial-info span {
  margin-right: 10px;
}
</style>